package br.com.localizagora.archive;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import br.com.localizagora.banco.Conexao;

public class AlarmDAO {
	private static PreparedStatement ps;
	private static Connection con;

	public static void insereAlarm(Alarm alarm){

		String sql = "INSERT INTO `localizagora`.`alarm`" 
												+"(`idDevice`,"
												+"`combustEletric`,"
												+"`tracking`,"
												+"`alarm`,"
												+"`charge`,"
												+"`acc`,"
												+"`voltage`,"
												+"`signal`,"
												+"`realDateTime`,"
												+"`dateTime`,"
												+"`latitude`,"
												+"`longitude`,"
												+"`velocidade`)"
									+"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?);";
		conectar(sql);

		try {

			ps.setInt(1,alarm.getIdDevice());
			ps.setBoolean(2, alarm.isOilEletricity());
			ps.setBoolean(3, alarm.isGpsTracking());
			ps.setInt(4, alarm.getAlarm());
			ps.setBoolean(5, alarm.isCharge());
			ps.setBoolean(6, alarm.isAcc());
			ps.setInt(7, alarm.getVoltageLevel());
			ps.setInt(8, alarm.getSignalLevel());
			ps.setTimestamp(9, alarm.getRealDateTime());
			ps.setTimestamp(10, alarm.getDateTime());	
			ps.setString(11,alarm.getLatitude());
			ps.setString(12,alarm.getLongitude());
			ps.setDouble(13,alarm.getSpeed());
	
			ps.execute();
			System.out.println("Alarm ok");
			fechar();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void conectar(String sql) {
		try {
			con = Conexao.getConnection();
			ps = con.prepareStatement(sql);
		} catch (SQLException ex) {
			Logger.getLogger(AlarmDAO.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	private static void fechar() {
		try {
			ps.close();
			con.close();
		} catch (SQLException ex) {
			Logger.getLogger(AlarmDAO.class.getName()).log(Level.SEVERE, null, ex);
		}
	}    
}
